<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="auto"
      v-show="showSearch"
    >
      <el-row>
        <el-form-item label="生产单编码" prop="produceNo">
          <el-input
            v-model.trim="queryParams.produceNo"
            style="width: 180px"
            placeholder="请输入生产单编码"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <!--                <el-form-item label="订单编码" prop="orderNo">-->
        <!--                    <el-input v-model.trim="queryParams.orderNo" style="width: 180px;" placeholder="请输入订单编码"-->
        <!--                        @keyup.enter="handleQuery" />-->
        <!--                </el-form-item>-->

        <el-form-item label="审核状态" prop="makeStatus">
          <el-select style="width: 180px" v-model="queryParams.makeStatus">
            <el-option label="未审核" value="1"></el-option>
            <el-option label="已审核" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="queryParams.type" style="width: 180px">
            <el-option label="封面" value="1"></el-option>
            <el-option label="内文" value="2"></el-option>
          </el-select>
        </el-form-item>
        <div class="operation">
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          <el-button @click="advanced = !advanced">
            <a style="color: #448ef7">
              <el-icon v-if="advanced">
                <ArrowUpBold />
              </el-icon>
              <el-icon v-else>
                <ArrowDownBold />
              </el-icon>
            </a>
          </el-button>
        </div>
      </el-row>
      <el-row v-if="advanced">
        <el-form-item label="车间" prop="workshopName">
          <el-select style="width: 180px" v-model="queryParams.workshopName">
            <el-option label="一车间" value="一车间"></el-option>
            <el-option label="四车间" value="四车间"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="新/旧版" prop="isNew">
          <el-select
            style="width: 180px"
            filterable
            v-model="queryParams.isNew"
            placeholder="请选择新/旧版"
          >
            <el-option
              v-for="item in ems_produce_is_new"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="书刊编码" prop="bookNo">
          <el-input
            v-model.trim="queryParams.bookNo"
            style="width: 180px"
            placeholder="请输入书刊编码"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="版材尺寸" prop="sheetSize">
          <el-select
            style="width: 180px"
            filterable
            v-model="queryParams.sheetSize"
            placeholder="请选择版材尺寸"
          >
            <el-option
              v-for="item in ems_machine_info_size"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="出版编号" prop="pubNo">
          <el-input
            v-model.trim="queryParams.pubNo"
            style="width: 180px"
            placeholder="请输入出版编号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="版材备注" prop="remark">
          <el-input
            v-model.trim="queryParams.remark"
            style="width: 180px"
            placeholder="请输入版材备注"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-button
        type="primary"
        :disabled="multiple"
        plain
        icon="Printer"
        @click="handlePrintList"
        >打印</el-button
      >
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>
    <div>
      <el-table
        v-loading="loading"
        border
        :data="dataList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" align="center" width="40" />
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          label="状态"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="status"
          v-if="columns[0].visible"
        >
          <template v-slot="{ row }">
            <dict-tag :options="ems_order_status" :value="row.status" />
          </template>
        </el-table-column>
        <el-table-column
          label="审核状态"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="makeStatus"
          v-if="columns[1].visible"
        >
          <template v-slot="{ row }">
            <el-tag :type="row.makeStatus == 1 ? 'danger' : ''" effect="dark">
              {{ row.makeStatus == 2 ? "已审核" : "未审核" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="数据状态"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="isSuspend"
          v-if="columns[0].visible"
        >
          <template v-slot="{ row }">
            <el-tag
              :type="row.isSuspend == 1 ? 'error' : 'success'"
              effect="dark"
              round
            >
              {{ row.isSuspend == 1 ? "暂停" : "正常" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="车间"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="workshopName"
          v-if="columns[0].visible"
        >
          <!-- <template v-slot="{ row }">
                        <dict-tag :options="ems_workshop" :value="row.workshopName" />
                    </template> -->
        </el-table-column>
        <el-table-column
          label="新/旧版"
          align="center"
          min-width="150"
          :show-overflow-tooltip="true"
          prop="isNew"
          v-if="columns[2].visible"
        >
          <template v-slot="{ row }">
            <dict-tag :options="ems_produce_is_new" :value="row.isNew" />
          </template>
        </el-table-column>
        <el-table-column
          label="版材备注"
          min-width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="remark"
        >
          <template v-slot="{ row }"
            >{{ row.changePiece }} {{ row.remark }}</template
          >
        </el-table-column>
        <el-table-column
          label="生产单编码"
          width="180"
          align="center"
          :show-overflow-tooltip="true"
          prop="orderNo"
          v-if="columns[1].visible"
        >
          <template v-slot="{ row }">
            <el-button type="primary" @click="handleView(row)" text>
              {{ row.produceNo }}
            </el-button>
          </template>
        </el-table-column>

        <el-table-column
          label="出版编号"
          min-width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="pubNo"
          v-if="columns[3].visible"
        ></el-table-column>
        <el-table-column
          label="产品名称"
          min-width="300"
          align="center"
          prop="productName"
          v-if="columns[3].visible"
        >
          <template v-slot="{ row }">
            <span>
              {{ row.productName === null ? row.bookName : row.productName }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="版材集合"
          width="120"
          align="center"
          prop="sheetSizeList"
        >
          <template v-slot="{ row }">
            <!-- sheetSizeList -->
            <!-- <dict-tag :options="ems_machine_info_size" :value="row.sheetSize" /> -->

            <dict-tag
              v-for="(item, index) in row.sheetSizeList?.split('+')"
              :key="index"
              :options="ems_machine_info_size"
              :value="item"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="印色集合"
          width="120"
          align="center"
          prop="printColor"
        >
          <template v-slot="{ row }">
            <div v-if="row.type == 1">
              <dict-tag
                :options="ems_order_print_color"
                :value="row.printColor"
              />
            </div>
            <div v-if="row.type == 2">
              <dict-tag
                v-for="(item, index) in row.printColor?.split(',')"
                :key="index"
                :options="ems_order_print_color"
                :value="item"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="订单数量"
          width="100"
          align="center"
          prop="orderCount"
          v-if="columns[3].visible"
        >
          <template v-slot="{ row }">
            <span>
              {{ row.orderCount === null ? row.produceCount : row.orderCount }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="书号"
          width="120"
          align="center"
          prop="bookNo"
        />
        <el-table-column
          label="下单日期"
          min-width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="submitDate"
          v-if="columns[4].visible"
        >
          <template v-slot="{ row }">
            {{ row.submitDate?.split("T")[0] ?? "" }}
          </template>
        </el-table-column>
        <el-table-column
          label="交货日期"
          min-width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="deliverDate"
          v-if="columns[5].visible"
        >
          <template v-slot="{ row }">
            {{ row.deliverDate?.split("T")[0] ?? "" }}
          </template>
        </el-table-column>
        <el-table-column
          label="加急交货日期"
          style="color: red"
          min-width="120"
          align="center"
          prop="urgentDate"
          v-if="columns[5].visible"
        >
          <!-- <template #header>
                        <span style="color: red">加急交货日期</span>
                    </template> -->
          <template v-slot="{ row }">
            <span style="color: red">
              {{ parseTime(row.urgentDate) }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="定价"
          width="80"
          align="center"
          prop="fixPrice"
        />
        <el-table-column
          label="出版社"
          width="120"
          align="center"
          prop="publishingHouse"
        />
        <el-table-column
          label="送货单位"
          width="120"
          align="center"
          prop="customName"
        />
        <el-table-column
          label="印张集合"
          width="120"
          align="center"
          prop="sheetList"
        />
        <!-- <el-table-column
          label="版材集合"
          width="120"
          align="center"
          prop="sheetSizeList"
        >
          <template v-slot="{ row }">
            <dict-tag
              v-for="(item, index) in row.sheetSizeList?.split('+')"
              :key="index"
              :options="ems_machine_info_size"
              :value="item"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="印色集合"
          width="120"
          align="center"
          prop="printColor"
        >
          <template v-slot="{ row }">
            <dict-tag
              v-for="(item, index) in row.printColor?.split(',')"
              :key="index"
              :options="ems_order_print_color"
              :value="item"
            />
          </template>
        </el-table-column> -->
        <el-table-column
          label="制单日期"
          width="120"
          align="center"
          prop="createTime"
        >
          <template v-slot="{ row }">
            {{ row.createTime.split("T")[0] }}
          </template>
        </el-table-column>

        <el-table-column
          label="印刷备注"
          min-width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="remark3"
        ></el-table-column>

        <el-table-column
          label="操作"
          align="center"
          width="150"
          fixed="right"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-tooltip
              content="审核"
              v-hasPermi="['versionRoom:audit']"
              placement="top"
            >
              <el-button
                link
                type="primary"
                v-if="scope.row.makeStatus != 2 && scope.row.status != -1"
                @click="handleUpdate(scope.row)"
              >
                审核
              </el-button>
            </el-tooltip>
            <!-- 无需做版 -->
            <!-- <el-tooltip content="审核"  v-hasPermi="['versionRoom:no:audit']" placement="top">
                            <el-button
                                link
                                type="primary"
                                v-if="scope.row.status == -1 && scope.row.makeStatus != 2"
                                @click="handleAudit(scope.row)"
                            >
                                审核
                            </el-button>
                        </el-tooltip> -->
            <!-- <el-tooltip content="驳回" placement="top">
                            <el-button
                                link
                                type="primary"
                                v-if="scope.row.makeStatus != 2"
                                @click="handleOverruled(scope.row)"
                            >
                                驳回
                            </el-button>
                        </el-tooltip>
                        <el-button
                            link
                            type="primary"
                            v-if="scope.row.status == 2 ||scope.row.status == 19 "
                            @click="handleNoMakePlate(scope.row)"
                        >
                            无需做版
                        </el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <printView
      :visible="printOpen"
      :printList="printList"
      @cancel="
        () => {
          printOpen = false;
          printList = [];
        }
      "
    ></printView>
  </div>
</template>

<script setup name="order">
import printView from "@/components/PrintView";
import {
  getProduceOrderList,
  updateMultiplestatus,
  updateMultipleStatus,
  updateMakeStatus,
} from "@/api/production/cover";

const { proxy } = getCurrentInstance();
const multiple = ref(true);
const selectRow = ref([]);
const printOpen = ref(false);
const printList = ref([]);
const loading = ref(true);
const advanced = ref(false);
const dataList = ref([]);
const showSearch = ref(true);
const total = ref(0);
const router = useRouter();
import { getToken } from "@/utils/auth";
/*** 用户导入参数 */
const upload = reactive({
  // 是否显示弹出层（用户导入）
  open: false,
  // 弹出层标题（用户导入）
  title: "",
  // 是否禁用上传
  isUploading: false,
  // 是否更新已经存在的用户数据
  updateSupport: 0,
  // 设置上传的请求头部
  headers: { Authorization: "Bearer " + getToken() },
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + "/mes/user/importMesUserInfo",
});
// 列显隐信息
const columns = ref([
  { key: 0, label: `状态`, visible: true },
  { key: 1, label: `审核状态`, visible: true },
  { key: 2, label: `生产单编码`, visible: true },
  { key: 3, label: `新/旧版`, visible: true },
  { key: 4, label: `出版编号`, visible: true },
  { key: 5, label: `下单日期`, visible: true },
  { key: 6, label: `加急交货日期`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    state: 2, // 过滤status>2
    makeStatus: "1",
    orderName: undefined,
    orderNo: undefined,
    isSuspend: "2",
  },
});
const { queryParams } = toRefs(data);
const {
  ems_order_print_color,
  ems_machine_info_size,
  ems_workshop_subject_type,
  ems_order_status,
  ems_produce_is_new,
  ems_workshop,
} = proxy.useDict(
  "ems_order_print_color",
  "ems_machine_info_size",
  "ems_order_status",
  "ems_workshop_subject_type",
  "ems_produce_is_new",
  "ems_workshop"
);
const getList = () => {
  loading.value = true;
  getProduceOrderList(queryParams.value)
    .then((res) => {
      loading.value = false;
      dataList.value = res.rows;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};
const handleSelectionChange = (row) => {
  selectRow.value = row;
  multiple.value = !row.length;
};
// 多选打印
const handlePrintList = () => {
  printOpen.value = true;
  printList.value = selectRow.value;
};
// 审核
const handleUpdate = (row) => {
  router.push({
    path: "/production/versionRoom/versionRoomDetail",
    query: {
      readonly: true,
      id: row.produceId,
    },
  });
};
const handleAudit = (row) => {
  proxy.$modal
    .confirm('是否确认审核"' + row.produceNo + '"生产单？')
    .then(function () {
      return updateMakeStatus({
        status: 19,
        makeStatus: 2,
        produceId: row.produceId,
      });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("审核成功");
    })
    .catch(() => {});
};
// 驳回
const handleOverruled = (row) => {
  proxy.$modal
    .confirm('是否确认驳回"' + row.produceNo + '"生产单？')
    .then(function () {
      return updateMultiplestatus({ ids: row.produceId, status: 1 });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("驳回成功");
    })
    .catch(() => {});
};
// 查看
const handleView = (row) => {
  router.push({
    path: "/production/versionRoom/versionRoomDetail",
    query: {
      id: row.produceId,
      info: true,
    },
  });
};
const handleNoMakePlate = (row) => {
  updateMultipleStatus({ status: "-1", ids: row.produceId }).then((res) => {
    getList();
  });
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};
getList();
// router.beforeEach((to, from, next) => {
//     if (to.path == "/basicInfo/personnel") {
//         getList()
//     }
//     next();
// });
</script>

<style lang="scss" scoped></style>
